// pages/login/login.js
const  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    ischeckagree:false,
    islogin:false,
    url:"",
    isautologin:true,
  },
  onGotUserInfo: function(e) {
    console.log(e)
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo && userInfo.openID){
      app.globalData.userInfo=userInfo;
    }
    console.log(app.globalData.userInfo);
    if( !app.globalData.userInfo.openID){
    
      
        
          var code =  app.globalData.userInfo.code;
          wx.getUserProfile({
            desc: '用于完善会员资料',
            success: res => {
              console.log(res)
              app.globalData.userInfo = res.userInfo;
              app.globalData.userInfo.iv = res.iv;
              app.globalData.userInfo.encryptedData = res.encryptedData;
              app.globalData.userInfo.code = code;
              
              that.wxlogin();
            },
            fail:res=>{
              console.log(res);
            }
          })
     
      /** 获取用户信息 **/
       /*
     
      */
     
    }
    else{
      console.log(app.globalData.userInfo);
      that.wxlogin();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var url = options.url;
      if(url){
        this.setData({url:url});
      }
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var pages = getCurrentPages();
    console.log(pages);
    for(var p in pages){
     // console.log(p);
      if(pages[p].route == 'pages/loading/index'){
       
        pages[p].onHide();
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.login({
      fail: function (res) {
        wx.showToast({
          title: res.res,
        })
      },
      success: function (res) {
        console.log(res);
        app.globalData.userInfo.code = res.code;
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  checkagree:function(){
    this.setData({
      ischeckagree:!this.data.ischeckagree,
    })
  },

  wxlogin:function(){
   var that = this;
   console.log(app.globalData.userInfo);
    if(this.data.ischeckagree){
      this.setData({islogin:true});
      app.wxlogin(app, this.frlogin,function(){
        
      }, app.globalData.userInfo, "") 
    }
    else{
      wx.showToast({
        title: '请勾选同意按钮',
        icon:'none',
      })
    }
  },

  frlogin:function(){
    
      this.setData({islogin:false});
      wx.setStorageSync('isautologin', this.data.isautologin);
      console.log( this.data.isautologin);
      if(this.data.isautologin){
        wx.setStorageSync('userInfo', app.globalData.userInfo);
      }
      if(this.data.url){
        wx.redirectTo({
          url: this.data.url,
        })
      }
      else{
        var pages = getCurrentPages();
        if(pages.length>1){
        wx.navigateBack({
          delta:-1,
        })
      //  var pages = getCurrentPages();
    console.log(pages);
      for(var p in pages){
      // console.log(p);
        if(pages[p].route == 'pages/loading/index'){
        
          pages[p].hidehomepic();
        }
        if(pages[p].route == 'pages/Tournament/match'){
          pages[p].getpagedate(false);
         }
        
       
      }
    }
  }
  },

  accountlogin:function(){
  var url =  './account/account' + ((this.data.url)?('?url='+this.data.url):'');
  console.log(url);
    wx.redirectTo({
     
      url:url,
    })
  },

  openusercontor:function(){
    wx.navigateTo({
      url: './usercontor/index',
    })
  },

  autologin:function(){
      this.setData({isautologin:!this.data.isautologin});
  }
  
})